home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Utilitare / Girder / girder331c.exe / {app} / luavolume.lua < prev    next >
Text File  |  2004-01-15  |  9KB  |  333 lines

  1.  
  2. -- Lua Volume Functions
  3. -- Copyright 2004 (c) Ron Bessems
  4. -- These volume make life easier for the Girder user
  5. -- All Constants are declared within the table LVF to avoid listing in the variable display window
  6. -- This file should not be modified
  7.  
  8. LVF = {}
  9. LVF.Version = 1.09 -- version info
  10.  
  11. -- DefaultMixer
  12. LVF.DefaultMixer = 0
  13.  
  14. --Destination Type Constants
  15.  
  16. LVF.DestinationTypeDestination = 0
  17. LVF.DestinationTypeSource = 1
  18.  
  19. --Source Type Constants
  20. LVF.SourceTypeMaster = -1
  21. LVF.SourceTypeWave = 0
  22.  
  23. --Control Type Constants
  24. -- this list includes common control tyes
  25.  
  26. LVF.ControlTypeBass = 1342373890
  27. LVF.ControlTypeEqualizer = 1342373892
  28. LVF.ControlTypeFader = 1342373888
  29. LVF.ControlTypeMute = 536936450
  30. LVF.ControlTypeMux = 1879113729
  31. LVF.ControlTypeOnOff = 536936449
  32. LVF.ControlTypeTrebel = 1342373891
  33. LVF.ControlTypeVolume = 1342373889
  34. LVF.ControlTypeLoudness = 536936452
  35.  
  36. -- Names -- for print purposes
  37. LVF.ControlTypes = {[1342373890] = "Bass", [536936450] = "Mute", [536936449] = "On/Off", [1342373891] = "Treble", [1342373889] = "Volume", [1879113729] = "Mux"}
  38.  
  39.  
  40. -- matches a control type to known listed devices above
  41. function GetControlTypeString(i)
  42.  return LVF.ControlTypes [i] or i     -- returns control number if unknown type
  43. end
  44.  
  45.  
  46. -- finds the requested control for a source
  47. -- returns nil if no matching control found
  48. function GetControlForSource (device,destination,source,controltype)
  49.   local control
  50.   for control=0, GetControlCount(device,destination,source)-1 do
  51.     if GetControlType(device,destination,source,control) == controltype then
  52.       return control
  53.     end
  54.   end
  55. end
  56.  
  57.  
  58. --Source Functions
  59.  
  60. -- Set the source volume 0 - 100 %
  61. function SetSourceVolume (device, destination, source, volume)
  62.   local control,type,lowerbound,upperbound,err,lvalue,rvalue,mvalue,offset,value
  63.  
  64.   control = GetControlForSource (device,destination,source,LVF.ControlTypeVolume)
  65.   if not control then
  66.     return -1
  67.   end
  68.  
  69.   if volume > 100 then
  70.     volume = 100
  71.   end
  72.   if volume < 0 then
  73.     volume = 0
  74.   end
  75.  
  76.   lowerbound,upperbound,type = GetControlBounds(device, destination,source,control)
  77.   if ( type < 0 ) then
  78.     return type
  79.   end
  80.  
  81.   value = (volume / 100 * (upperbound-lowerbound)) + lowerbound
  82.  
  83.   -- preserve the balance
  84.   lvalue,err = GetChannelValue (device ,destination,source,control,0,0)
  85.   rvalue,err = GetChannelValue (device ,destination,source,control,1,0)
  86.  
  87.   if ( lvalue > rvalue ) then
  88.     if ( lvalue == 0 ) then
  89.       -- should not happen in an unsigned channel...
  90.     else
  91.       mvalue = lvalue
  92.       offset = rvalue / lvalue
  93.  
  94.       lvalue = value
  95.       rvalue = value * offset
  96.     end
  97.   else
  98.     if ( rvalue==lvalue) then
  99.       lvalue=value
  100.       rvalue=value
  101.     else
  102.       if ( rvalue == 0 ) then
  103.         rvalue=rvalue
  104.         lvalue=0
  105.       else
  106.         mvalue = rvalue
  107.         offset = lvalue / rvalue
  108.         rvalue = value
  109.         lvalue = value * offset
  110.       end
  111.     end
  112.   end
  113.  
  114.   -- set left channel
  115.   err = SetChannelValue(device ,destination,source,control,0,0,lvalue)
  116.  
  117.   if ( err <0 ) then
  118.     return err
  119.   end
  120.  
  121.   -- set right channel
  122.   return SetChannelValue(device ,destination,source,control,1,0,rvalue)
  123. end
  124.  
  125. -- Get the source volume in %
  126. function GetSourceVolume (device, destination, source)
  127.   local err,type,lowerbound,upperbound,value
  128.  
  129.   lowerbound, upperbound, type = GetControlBounds (device,destination,source,GetControlForSource (device,destination,source,LVF.ControlTypeVolume) or 0)
  130.   if ( type < 0 ) then
  131.     return 0,type
  132.   end
  133.  
  134.   value,err = GetChannelValue(device ,destination,source,GetControlForSource (device,destination,source,LVF.ControlTypeVolume) or 0,-1,0)
  135.   if err < 0 then
  136.     return 0,err
  137.    else
  138.     return round (((value-lowerbound) /(upperbound-lowerbound) * 100),0),err
  139.   end
  140. end
  141.  
  142. -- Set Mute for Source (1=mute, 0=unmute)
  143. function SetSourceMute (device, destination, source,mute)
  144.   local control,type,lowerbound,upperbound,value
  145.  
  146.   control = GetControlForSource (device,destination,source,LVF.ControlTypeMute)
  147.   if not control then
  148.     return -1
  149.   end
  150.  
  151.   if mute > 1 then
  152.     mute = 1
  153.   end
  154.   if mute < 0 then
  155.     mute = 0
  156.   end
  157.  
  158.   lowerbound,upperbound, type = GetControlBounds (device, destination,source,control)
  159.  
  160.   if ( type < 0 ) then
  161.     return type
  162.   end
  163.  
  164.   value = (mute * (upperbound-lowerbound)) + lowerbound
  165.  
  166.   return SetChannelValue (device ,destination,source,control,-1,0,value)
  167. end
  168.  
  169. -- Get control Mute
  170. function GetSourceMute (device,destination,source)
  171.   return GetChannelValue (device ,destination,source,GetControlForSource (device,destination,source,LVF.ControlTypeMute) or 1,-1,0)
  172. end
  173.  
  174.  
  175. -- Control Level Functions
  176.  
  177. function SetControlBalance (device, destination, source, control, balance)
  178. --  - 100 = left
  179. --  + 100 = right
  180. --  0     = center
  181.   local balance,err,mvalue,lvalue,rvalue
  182.  
  183.   if balance > 100 then
  184.     balance = 100
  185.   end
  186.  
  187.   if balance < -100 then
  188.     balance = -100
  189.   end
  190.  
  191.   -- first get the max value
  192.   mvalue,err = GetChannelValue (device, destination, source, control, -1,0)
  193.   if (err<0) then
  194.     return err
  195.   end
  196.  
  197.   -- now determine left and right value
  198.   if ( balance < 0 ) then
  199.     balance = balance + 100
  200.     lvalue = mvalue
  201.     rvalue = mvalue * balance / 100
  202.   else
  203.     balance = 100 - balance
  204.     lvalue = mvalue * balance / 100
  205.     rvalue = mvalue
  206.   end
  207.  
  208.   err = SetChannelValue (device ,destination,source,control,0,0,lvalue)
  209.   if ( err <0 ) then
  210.     return err
  211.   end
  212.   return SetChannelValue (device ,destination,source,control,1,0,rvalue)
  213. end
  214.  
  215.  
  216.  
  217. -- Quick and Easy Functions
  218.  
  219. -- 1. Master on Default Mixer
  220.  
  221. -- Set the master volume 0 - 100 %
  222. function SetMasterVolume (volume)
  223.   return SetSourceVolume (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeMaster,volume)
  224. end
  225.  
  226. -- Get Master Volume %
  227. function GetMasterVolume ()
  228.   return GetSourceVolume (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeMaster)
  229. end
  230.  
  231. -- Mutes the master channel (1=mute, 0=unmute)
  232. function SetMasterMute (mute)
  233.   return SetSourceMute (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeMaster,mute)
  234. end
  235.  
  236. -- Get Master Mute
  237. function GetMasterMute ()
  238.   return GetSourceMute (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeMaster)
  239. end
  240.  
  241.  
  242. -- 2. Wave on Default Mixer
  243.  
  244. -- Set the wave volume 0 - 100 %
  245. function SetWaveVolume (volume)
  246.   return SetSourceVolume (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeWave,volume)
  247. end
  248.  
  249. -- Get Wave Volume %
  250. function GetWaveVolume ()
  251.   return GetSourceVolume (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeWave)
  252. end
  253.  
  254. -- Mutes the wave channel (1=mute, 0=unmute)
  255. function SetWaveMute (mute)
  256.   return SetSourceMute (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeWave,mute)
  257. end
  258.  
  259. -- Get Wave Mute
  260. function GetWaveMute ()
  261.   return GetSourceMute (LVF.DefaultMixer,LVF.DestinationTypeDestination,LVF.SourceTypeWave)
  262. end
  263.  
  264.  
  265. -- Display Entire Mixer
  266.  
  267. function MixerPrintDetails (device)
  268.   local DestCnt,i,SrcCnt,err,name,CtrlCnt,k,type,s,j,ItemCnt,l,value
  269.  
  270.   device = device or LVF.DefaultMixer
  271.  
  272.   name,err = GetDeviceName (device)
  273.  
  274.   if err < 0 then
  275.     print ("Error opening sound card: ",device)
  276.     return
  277.   end
  278.  
  279.   print ("Enumerating Sound Card Controls for device number ",device,".")
  280.   print ("")
  281.   print (name)
  282.  
  283.   DestCnt = GetDestinationCount(device)
  284.   if ( DestCnt <0 ) then
  285.     print ("Error opening sound card:"..device)
  286.     return
  287.   end
  288.  
  289.   for i=0,DestCnt-1 do
  290.  
  291.     name,err = GetDestinationName (device, i)
  292.     print ("  ",name)
  293.  
  294.     -- request sourcecount of the current destination (i)
  295.     SrcCnt = GetSourceCount (device, i)
  296.  
  297.     -- start at -1 to include the master channel as well
  298.     for j=-1, SrcCnt-1 do
  299.  
  300.       -- now we are accessing the name of the source, this needs
  301.       -- two indices i and j.
  302.       name,err = GetSourceName (device, i,j)
  303.       print ("    "..name) -- indented for clarity
  304.  
  305.       -- get the number of controls on the current source
  306.       CtrlCnt = GetControlCount (device, i,j)
  307.  
  308.       for k=0, CtrlCnt-1 do
  309.  
  310.         name,err = GetControlName (device,i,j,k)
  311.         type = GetControlType (device,i,j,k)
  312.         s = GetControlTypeString (type)
  313.         value,err = GetChannelValue (device,i,j,k, -1,0)
  314.         print ("      "..name.."  (" .. s.." control) value: ",value)
  315.         ItemCnt = GetItemCount (device,i,j,k)
  316.  
  317.         for l=0, ItemCnt-1 do
  318.           name,err = GetItemName (device,i,j,k,l)
  319.           value,err = GetChannelValue (device,i,j,k,0,l)
  320.           print ("        *"..name..", Value: " ..value)
  321.         end
  322.       end
  323.     end
  324.   end
  325.   CloseVolumeControls(device)
  326. end
  327.  
  328. print ("LUA Volume Functions Loaded, Version: ",LVF.Version)
  329.  
  330.  
  331.  
  332.  
  333.